import os
from typing import Optional
from pydantic_settings import BaseSettings
from pydantic import Field

class Settings(BaseSettings):
    """项目配置类"""
    
    # 服务配置
    HOST: str = Field(default="0.0.0.0", env="HOST")
    PORT: int = Field(default=8000, env="PORT")
    RELOAD: bool = Field(default=True, env="RELOAD")
    WORKERS: int = Field(default=1, env="WORKERS")
    LOG_LEVEL: str = Field(default="info", env="LOG_LEVEL")
    
    # 模型配置
    DEFAULT_MODEL_NAME: str = Field(default="Qwen2.5-7B-Instruct", env="DEFAULT_MODEL_NAME")
    HF_MODEL_PATH: str = Field(default="Qwen/Qwen2.5-7B-Instruct", env="HF_MODEL_PATH")
    HF_CACHE_DIR: str = Field(default="~/.cache/huggingface", env="HF_CACHE_DIR")
    DEVICE: str = Field(default="cuda", env="DEVICE")  # cuda or cpu
    MAX_LENGTH: int = Field(default=4096, env="MAX_LENGTH")
    TEMPERATURE: float = Field(default=0.7, env="TEMPERATURE")
    TOP_P: float = Field(default=0.95, env="TOP_P")
    
    # 微调配置
    FINETUNE_OUTPUT_DIR: str = Field(default="./finetune_output", env="FINETUNE_OUTPUT_DIR")
    MAX_TRAINING_STEPS: int = Field(default=1000, env="MAX_TRAINING_STEPS")
    BATCH_SIZE: int = Field(default=4, env="BATCH_SIZE")
    LEARNING_RATE: float = Field(default=2e-5, env="LEARNING_RATE")
    
    # RAG配置
    RAG_DATA_DIR: str = Field(default="./rag_data", env="RAG_DATA_DIR")
    RAG_INDEX_DIR: str = Field(default="./rag_index", env="RAG_INDEX_DIR")
    CHUNK_SIZE: int = Field(default=1024, env="CHUNK_SIZE")
    CHUNK_OVERLAP: int = Field(default=128, env="CHUNK_OVERLAP")
    
    # API配置
    API_KEY: Optional[str] = Field(default=None, env="API_KEY")
    ENABLE_AUTH: bool = Field(default=False, env="ENABLE_AUTH")
    
    # Redis配置（用于缓存）
    REDIS_HOST: Optional[str] = Field(default=None, env="REDIS_HOST")
    REDIS_PORT: int = Field(default=6379, env="REDIS_PORT")
    
    # Ollama配置
    OLLAMA_API_BASE_URL: str = Field(default="http://localhost:11434/v1", env="OLLAMA_API_BASE_URL")
    ENABLE_OLLAMA: bool = Field(default=False, env="ENABLE_OLLAMA")
    
    class Config:
        """配置元数据"""
        env_file = ".env"
        env_file_encoding = "utf-8"
        case_sensitive = True

# 创建配置实例
settings = Settings()